﻿using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using DQS.BLL;
using DQS.EL;

public partial class Operate_PayFeePhase : DQS_Page
{
    private decimal QueryID
    {
        get { return Decimal.Parse(Request.QueryString["queryid"].ToString()); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetQueryID();
            Attribute_Bind();
        } 
    }

    private void Attribute_Bind()
    {
        Begin_Date.Attributes.Add("onclick", "WdatePicker({el:$dp.$('BeginDate'),maxDate:'%y-%M-%d',minDate:'2011-01-01'});");
        End_Date.Attributes.Add("onclick", "WdatePicker({el:$dp.$('EndDate'),maxDate:'%y-%M-%d',minDate:'2011-01-01'})");
    }

    protected void SaveCondition_Click(object sender, EventArgs e)
    {
        DQ_Income_Phase income = new DQ_Income_Phase();
        income.Query_ID = QueryID;
        income.Begin_date = Begin_Date.Text.Trim().Length == 0 ? DateTime.MinValue : DateTime.Parse(Begin_Date.Text.Trim());
        if (End_Date.Text.Trim().Length == 0)
        {
            income.End_date = DateTime.Now;
        }
        else
        {
            income.End_date = DateTime.Parse(End_Date.Text.Trim()).AddDays(1);
        }
        DQ_Income_PhaseManaher.IncomePhaseBuild(income);
        Response.Redirect("~/Operate/ShowData.aspx?queryid=" + QueryID.ToString());
    }

    protected void UploadFile_Click(object sender, EventArgs e)
    {
        if (ServiceIdImport.HasFile)
        {
            ServiceIdImport.PostedFile.SaveAs(Server.MapPath("~/Upload/ServiceID/") + QueryID.ToString() + ".txt");
            ServiceIdImport.Enabled = false;
            ValidatorTXTForServiceID.Enabled = false;
            UploadFile.Enabled = false;
            UploadFile.Text = "已上传";
            ServiceIDInDB();
        }
        else
        {
            JQueryAlert("未选择任何文件");
        }
    }

    private void ServiceIDInDB()
    {
        IList<DQ_SERVICEID> sidList = new List<DQ_SERVICEID>();
        using (StreamReader sr = new StreamReader(Server.MapPath("~/Upload/ServiceID/") + QueryID.ToString() + ".txt"))
        {
            string line = null;
            while ((line = sr.ReadLine()) != null)
            {
                if (line.Trim().Length > 0)
                {
                    DQ_SERVICEID sid = new DQ_SERVICEID();
                    sid.QUERY_ID = QueryID;
                    sid.SERVICE_ID = line.Trim();
                    sidList.Add(sid);
                }
            }
        }
        if (sidList.Count > 0)
        {
            DQ_ServiceIDManager.InsertServiceIDs(sidList);
        }
    }

    private void SetQueryID()
    {
        if (Request.QueryString["QueryID"] == null)
        {
            DQ_USERS user = Session["LoginUser"] as DQ_USERS;
            Response.Redirect("~/Operate/PayFeePhase.aspx?queryid=" + (DQ_QUERYManager.NewQueryID(Convert.ToInt32(user.USER_ID)).ToString()));
        }
    }
}
